# encoding: utf-8
# @File  : page_ht_ProductList.py
# @Author: wu shaofan
# @Date  :  2024/04/29
# @Desc :
from selenium.webdriver.common.by import By
from PO.base.base import Base


class PagehtProductList(Base):
    # 商城tab菜单
    shop_tab = By.LINK_TEXT, "商城"
    # iframe 框架
    iframe1_loc = By.ID, 'workspace'
    # 添加商品按钮
    add_product_button = By.CSS_SELECTOR, 'div[title="添加商品"] span '
    # 商品名称
    product_name = By.NAME, 'goods_name'
    # 商品分类下拉框--一级分类
    product_category_level1 = By.ID, 'cat_id'
    product_category_level1_option = By.CSS_SELECTOR, 'select[id="cat_id"] option'
    # 商品分类下拉框--二级分类
    product_category_level2 = By.ID, 'cat_id_2'
    product_category_level2_option = By.CSS_SELECTOR, 'select[id="cat_id_2"] option'
    # 商品分类下拉框--三级分类
    product_category_level3 = By.ID, 'cat_id_3'
    product_category_level3_option = By.CSS_SELECTOR, 'select[id="cat_id_3"] option'
    # 商品简介
    product_introduction = By.NAME, 'goods_remark'
    # 商品spu
    product_spu = By.NAME, 'spu'
    # 商品sku
    product_sku = By.NAME, 'sku'
    # 商品品牌
    product_brand = By.ID, 'brand_id'
    product_brand_option = By.CSS_SELECTOR, 'select[id="brand_id"] option'
    # 供应商
    product_supplier = By.ID, 'suppliers_id'
    product_supplier_option = By.CSS_SELECTOR, 'select[id="suppliers_id"] option'
    # 本店售价
    product_price = By.NAME, 'shop_price'
    # 商品市场价
    product_market_price = By.NAME, 'market_price'
    # 商品成本价
    product_cost_price = By.NAME, 'cost_price'
    # 商品佣金
    product_commission = By.NAME, 'commission'
    # 上传图片
    product_image_loc1 = By.CLASS_NAME, 'type-file-file'
    iframe5_loc = By.CSS_SELECTOR, 'div[class="layui-layer-content"] iframe'
    product_image_loc = By.CSS_SELECTOR, 'div[id="filePicker"] input'
    upload_success = By.CLASS_NAME, 'success'
    upload_success_btn = By.CLASS_NAME, 'saveBtn'
    # 商品标签
    product_label = By.NAME, 'label_id'
    product_label_option = By.CSS_SELECTOR, 'select[name="label_id"] option'
    # 商品重量
    product_weight = By.NAME, 'weight'
    # 商品体积
    product_volume = By.NAME, 'volume'
    # 选择运费模版
    product = By.CSS_SELECTOR, 'dd[class="opt"] div[class="freight_template"]'
    product_freight_template = By.CSS_SELECTOR, 'div[class="freight_template"]  select'
    product_freight_template_option = By.CSS_SELECTOR, 'select[name="template_id"] option'
    # 输入商品关键词
    product_keywords = By.NAME, 'keywords'
    # 虚拟销售量
    product_fictitious_sales = By.NAME, 'virtual_sales_sum'
    # 虚拟收藏量
    product_fictitious_collect = By.NAME, 'virtual_collect_sum'
    # 商品描述
    iframe3_loc = By.CSS_SELECTOR, 'div[class="edui-editor-iframeholder edui-default"] iframe'
    product_description = By.CSS_SELECTOR, 'html[class="view"] body'
    # 保存按钮
    product_save_button = By.LINK_TEXT, '确认提交'
    # toast信息
    toast_loc1 = By.CSS_SELECTOR, '.layui-layer-content.layui-layer-padding'
    # 商品列表搜索框
    search_input = By.NAME, 'key_word'
    # 搜索按钮
    search_button = By.CLASS_NAME, 'btn'
    # 全选按钮
    select_all_button = By.CSS_SELECTOR, 'th[class="sign"] i'
    # 商品列表中勾选框按钮
    select_button = By.CSS_SELECTOR, 'td[class="sign"] i'
    # 商品名称
    product_name_loc = By.CSS_SELECTOR, 'td[align="center"] div[style="text-align: left; width: 300px;"]'
    # 批量删除
    batch_delete_button = By.CSS_SELECTOR, 'div[title="批量删除"] span'
    # 确认删除按钮
    confirm_delete_button = By.CLASS_NAME, 'layui-layer-btn0'
    
    # 点击商城tab菜单
    def page_ht_click_shop_tab(self):
        self.base_click(self.shop_tab)
        self.base_get_allure_image()

    # 进入iframe框架
    def page_ht_enter_iframe(self):
        self.base_if_frame_exist(self.iframe1_loc)
        self.base_get_allure_image()

    # 点击添加商品按钮
    def page_ht_click_add_product_btn(self):
        self.base_click(self.add_product_button)
        self.base_get_allure_image()

    # 输入商品名称
    def page_ht_input_product_name(self, product_name):
        self.base_input(self.product_name, product_name)
        self.base_get_allure_image()

    # 下拉框选择一级商品分类
    def page_ht_select_product_category01(self):
        return self.base_new_select(self.product_category_level1, self.product_category_level1_option, "没找到一级商品分类")

    # 下拉框选择二级商品分类
    def page_ht_select_product_category02(self):
        return self.base_new_select(self.product_category_level2, self.product_category_level2_option, "没找到二级商品分类")

    # 下拉框选择三级商品分类
    def page_ht_select_product_category03(self):
        return self.base_new_select(self.product_category_level3, self.product_category_level3_option, "没找到三级商品分类")

    # 商品简介
    def page_ht_input_product_introduction(self, product_introduction):
        self.base_input(self.product_introduction, product_introduction)
        self.base_get_allure_image()

    # 输入商品SPU
    def page_ht_input_product_spu(self, product_spu):
        self.base_input(self.product_spu, product_spu)
        self.base_get_allure_image()

    # 输入商品SKU
    def page_ht_input_product_sku(self, product_sku):
        self.base_input(self.product_sku, product_sku)
        self.base_get_allure_image()

    # 选择商品品牌
    def page_ht_select_product_brand(self):
        return self.base_new_select(self.product_brand, self.product_brand_option, "没找到商品品牌")

    # 选择供应商
    def page_ht_select_product_supplier(self):
        return self.base_new_select(self.product_supplier, self.product_supplier_option, "没找到供应商")

    # 输入商品价格
    def page_ht_input_product_price(self, product_price):
        self.base_input(self.product_price, product_price)
        self.base_get_allure_image()

    # 输入商品市场价
    def page_ht_input_product_market_price(self, product_market_price):
        self.base_input(self.product_market_price, product_market_price)
        self.base_get_allure_image()

    # 输入商品成本价
    def page_ht_input_product_cost_price(self, product_cost_price):
        self.base_input(self.product_cost_price, product_cost_price)
        self.base_get_allure_image()

    # 输入商品佣金
    def page_ht_input_product_commission(self, product_commission):
        self.base_input(self.product_commission, product_commission)
        self.base_get_allure_image()

    # 点击上传图片按钮
    def page_ht__upload_product_image_btn(self):
        self.base_find_elements(self.product_image_loc1)[0].click()
        self.base_get_allure_image()

    # 后台商品管理页面弹框的iframe5框架-
    def page_ht_to_iframe5(self):
        self.base_if_frame_exist(self.iframe5_loc)

    # 上传图片
    def page_ht_upload_product_image(self, filepath):
        self.base_find_element(self.product_image_loc).send_keys(filepath)

    # 等待上次成功
    def page_ht_upload_success(self):
        self.base_if_element_exist(self.upload_success)

    # 点击确定按钮
    def page_ht_click_upload_success_btn(self):
        self.base_click(self.upload_success_btn)
        self.base_get_allure_image()

    # 选择商品标签
    def page_ht_select_product_label(self):
        return self.base_new_select(self.product_label, self.product_label_option, "没找到商品标签")

    # 输入商品重量
    def page_ht_input_product_weight(self, product_weight):
        self.base_input(self.product_weight, product_weight)
        self.base_get_allure_image()

    # 输入商品体积
    def page_ht_input_product_volume(self, product_volume):
        self.base_input(self.product_volume, product_volume)
        self.base_get_allure_image()

    # 选择运费模版
    def page_ht_select_product_freight_template(self):
        # 判断元素是否可见  Ture 表示可见
        displayed = self.base_find_element(self.product).is_displayed()
        if displayed:
            return self.base_new_select(self.product_freight_template, self.product_freight_template_option, "没找到运费模版")
        else:
            return "选择了包邮，不需要选择运费模版"

    # 输入商品关键词
    def page_ht_input_product_keywords(self, product_keywords):
        self.base_input(self.product_keywords, product_keywords)
        self.base_get_allure_image()

    # 输入商品虚拟销售量
    def page_ht_input_product_fictitious_sales(self, product_virtual_sales):
        self.base_input(self.product_fictitious_sales, product_virtual_sales)
        self.base_get_allure_image()

    # 输入商品虚拟收藏量
    def page_ht_input_product_fictitious_collect(self, product_virtual_collect):
        self.base_input(self.product_fictitious_collect, product_virtual_collect)
        self.base_get_allure_image()

    # 进入商品描述iframe
    def page_ht_enter_iframe3(self):
        self.base_if_frame_exist(self.iframe3_loc)
        self.base_get_allure_image()

    def page_ht_input_product_description(self, product_description):
        self.base_input(self.product_description, product_description)
        self.base_get_allure_image()

    # 点击保存按钮
    def page_ht_click_save_btn(self):
        self.base_click(self.product_save_button)
        self.base_get_allure_image()

    # 获取toast信息
    def page_ht_get_toast_info(self):
        return self.base_get_toast_text(self.toast_loc1)

    # 搜索框输入商品名称
    def page_ht_search_product_name(self, product_name):
        self.base_input_no_clear(self.search_input, product_name)
        self.base_get_allure_image()

    # 点击搜索按钮
    def page_ht_click_search_button(self):
        self.base_click(self.search_button)
        self.base_get_allure_image()

    # 判断商品列表中商品勾选框的个数
    def page_ht_get_product_select_count(self):
        try:
            count = len(self.base_find_elements(self.select_button))
            return count
        except:
            return 0

    # 获取商品列表中的商品名称
    def page_ht_get_product_name_list(self):
        try:
            return self.base_get_text_list(self.product_name_loc)
        except:
            return []

    # 点击全选按钮
    def page_ht_click_select_all_button(self):
        self.base_click(self.select_all_button)
        self.base_get_allure_image()

    # 点击批量删除按钮
    def page_ht_click_batch_delete_button(self):
        self.base_click(self.batch_delete_button)
        self.base_get_allure_image()

    # 点击确定删除按钮
    def page_ht_click_confirm_delete_button(self):
        self.base_click(self.confirm_delete_button)
        self.base_get_allure_image()
